//
//  HOTLoginViewController.m
//  Genpara
//
//  Created by AndyChang.zhang on 14-8-27.
//  Copyright (c) 2014年 zhangchh. All rights reserved.
//

#import "HOTLoginViewController.h"
#import "HOTFindPwdViewController.h"
#import "HOTRegisterViewController.h"
#import "HOTLoginModel.h"
#import "HOTResultModel.h"
#import "HOTMenuViewController.h"
#import "ZZZPopupView.h"
#import "UITextField+extend.h"


@interface HOTLoginViewController () <UITextFieldDelegate> {
    
    HOTLoginModel *loginModel;
}

@end

@implementation HOTLoginViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setNaviTitle:@"ログイン"];
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITextField Delegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];
    
    return YES;
}


- (IBAction)autoLoginBtnClicked:(UIButton *)sender {
    
    sender.selected = !sender.isSelected;
}

- (IBAction)resetPwdBtnclicked:(id)sender {
    
    HOTFindPwdViewController *fpvc = [[HOTFindPwdViewController alloc] initWithNibName:@"HOTFindPwdViewController" bundle:nil];
    
    [self.navigationController pushViewController:fpvc animated:YES];
}

- (IBAction)sendBtnClicked:(id)sender {
    
    [self sendRequest];
}

-(void)sendRequest {
    
    if (![self checkParametersValid]) {
        
        //参数检查
        return;
    }
    
    [self showLoadingWithMessage:MSG_LOGINING];
    
    NSDictionary *parameters = @{REQ_KEY_USER_EMAIL:_emailText.text,
                                 REQ_KEY_USER_PWD:_passwordText.text,};
    
    __unsafe_unretained HOTLoginViewController *tempObj = self;
    [self requestWithURL:HOT_URL_LOGIN Parameters:parameters success:^(NSInteger result, NSDictionary * data) {
        
        NSLog(@"%@", @"登陆成功");
        
        [tempObj.userInfo loginWithDic:data withAutologin:_autoLoginBtn.isSelected];
        
        [tempObj showSuccessWithMessage:MSG_LOGIN_SUCCESS complection:^{
            
            if ([self.delegate respondsToSelector:@selector(loginSuccessed)]) {
                [self.delegate loginSuccessed];
            }
            
//            HOTMenuViewController *menuVC = [[HOTMenuViewController alloc] initWithNibName:@"HOTMenuViewController" bundle:nil];
//            
//            [tempObj.navigationController pushViewController:menuVC animated:YES];
        }];
        
    } failure:^(NSError *error, NSString *errMsg) {
        
        [tempObj loadingSuccess];
        if ([tempObj checkErrResult:error errMsg:errMsg]) {
            return ;
        }
        if (errMsg.length > 0) {
            
            ZZZPopupView *popupView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE message:errMsg delegate:nil cancelButtonTitle:nil otherButtonTitle:ALERT_BTN_OK];
            
            [popupView showInWindow];
        }
    }];
}

-(BOOL)checkParametersValid {
    
    NSString *errName = @"";
    HOTInputItemType inputType = 0;
    if ([_emailText textIsNullOrEmpty]) {
        
        errName = @"メールアドレス";
        inputType = HOTInputItemText;
    } else if ([_passwordText textIsNullOrEmpty]) {
        
        errName = @"パスワード";
        inputType = HOTInputItemText;
    }
    
    if (errName.length > 0) {
        
        NSString * errMsg = [self getErrMsgWith:errName itemType:inputType];
        
        ZZZPopupView *popupView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE message:errMsg delegate:nil cancelButtonTitle:nil otherButtonTitle:ALERT_BTN_OK];
        
        [popupView showInWindow];
        
        return NO;
    }

    return YES;
}


-(void)plist {
    
    NSArray *tempArr =@[
    @{@"rid":@"1",@"name":@"北海道"},
    @{@"rid":@"2",@"name":@"青森県"},
    @{@"rid":@"3",@"name":@"秋田県"},
    @{@"rid":@"4",@"name":@"岩手県"},
    @{@"rid":@"5",@"name":@"山形県"},
    @{@"rid":@"6",@"name":@"宮城県"},
    @{@"rid":@"7",@"name":@"福島県"},
    @{@"rid":@"8",@"name":@"新潟県"},
    @{@"rid":@"9",@"name":@"富山県"},
    @{@"rid":@"10",@"name":@"石川県"},
    @{@"rid":@"11",@"name":@"群馬県"},
    @{@"rid":@"12",@"name":@"栃木県"},
    @{@"rid":@"13",@"name":@"長野県"},
    @{@"rid":@"14",@"name":@"岐阜県"},
    @{@"rid":@"15",@"name":@"埼玉県"},
    @{@"rid":@"16",@"name":@"茨城県"},
    @{@"rid":@"17",@"name":@"東京都"},
    @{@"rid":@"18",@"name":@"千葉県"},
    @{@"rid":@"19",@"name":@"神奈川県"},
    @{@"rid":@"20",@"name":@"静岡県"},
    @{@"rid":@"21",@"name":@"山梨県"},
    @{@"rid":@"22",@"name":@"愛知県"},
    @{@"rid":@"23",@"name":@"福井県"},
    @{@"rid":@"24",@"name":@"滋賀県"},
    @{@"rid":@"25",@"name":@"三重県"},
    @{@"rid":@"26",@"name":@"京都府"},
    @{@"rid":@"27",@"name":@"奈良県"},
    @{@"rid":@"28",@"name":@"兵庫県"},
    @{@"rid":@"29",@"name":@"大阪府"},
    @{@"rid":@"30",@"name":@"和歌山県"},
    @{@"rid":@"31",@"name":@"島根県"},
    @{@"rid":@"32",@"name":@"鳥取県"},
    @{@"rid":@"33",@"name":@"岡山県"},
    @{@"rid":@"34",@"name":@"広島県"},
    @{@"rid":@"35",@"name":@"山口県"},
    @{@"rid":@"36",@"name":@"香川県"},
    @{@"rid":@"37",@"name":@"愛媛県"},
    @{@"rid":@"38",@"name":@"徳島県"},
    @{@"rid":@"39",@"name":@"高知県"},
    @{@"rid":@"40",@"name":@"大分県"},
    @{@"rid":@"41",@"name":@"福岡県"},
    @{@"rid":@"42",@"name":@"佐賀県"},
    @{@"rid":@"43",@"name":@"熊本県"},
    @{@"rid":@"44",@"name":@"長崎県"},
    @{@"rid":@"45",@"name":@"宮崎県"},
    @{@"rid":@"46",@"name":@"鹿児島県"},
    @{@"rid":@"47",@"name":@"沖縄県"}];
    
    NSMutableArray *multeArr = [NSMutableArray arrayWithArray:tempArr];
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSLog(@"%@", [paths firstObject]);
    
    BOOL succ=[multeArr writeToFile:[NSString stringWithFormat:@"%@/regonList.plist", [paths firstObject]] atomically:YES];
    NSLog(@"%d", succ);
}

@end
